package cn.com.pajx.pajx_visitor_v3.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder holder;
    private Paint paint;
    private RectF oval;

    public CameraSurfaceView(Context context) {
        super(context);
        init(context);
    }

    public CameraSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CameraSurfaceView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        holder = getHolder();
        holder.addCallback(this);
        paint = new Paint();
        paint.setAntiAlias(true);
        setFocusable(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 确保视图的宽度和高度最大值为半径
        int radius = Math.max(w, h) / 2;
        // 设置圆的绘制区域
        oval = new RectF(-radius, -radius, radius, radius);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Canvas canvas = holder.lockCanvas();
        drawCircle(canvas);
        holder.unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    private void drawCircle(Canvas canvas) {
        canvas.translate(getWidth() / 2, getHeight() / 2); // 移动到视图中心
        paint.setColor(0xFF000000); // 设置圆的背景颜色
        canvas.drawOval(oval, paint); // 绘制背景圆
        paint.setColor(0xFFFF0000); // 设置圆形预览的颜色
        canvas.drawArc(oval, -90, 360, true, paint); // 绘制实时的旋转圆形预览
    }

}
